package engine.logic
{
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.display.newAttack.action.effect.AttackTrack;
	import engine.flag.PlayerActionFlag;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.pool.ObjectPool;
	import engine.scene.SceneManager;
	import engine.scene.ScenePlayerLogic;
	import engine.scene.player.BasePlayerModel;
	import engine.scene.player.NpcModel;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.utils.getTimer;
	import newx.StageInfo;
	
	/**
	 * 雪人逻辑 
	 * @author gf
	 * 
	 */	
	
	public class SnowLogic
	{
		private static	 var _self			:SnowLogic;
		private var model					:ModelLocator;
		private var lastTime				:int;
		private var gapTime				:int	=	3000;
		private var gapNumTime				:int	=	1000;
		public var isBomb					:Boolean=false;
		
		public static function getInstance():SnowLogic
		{
			if ( _self == null )
			{
				_self = new SnowLogic();
			}
			return _self;
		}
		
		public function SnowLogic()
		{
			init();
		}
		
		private function init():void
		{
			model	=	ModelLocator.getInstance();
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SHOOT_SNOWMAN),onSnowAttack);
		}
		
		private function onSnowAttack(e:CustomMsgEvent):void
		{
			var arr:Array	=	e.pList;
			if(arr.length==0) return;
			var point:Point	=	new Point(StageInfo.effectSpace.x,StageInfo.effectSpace.y);
			var globalPoint:Point	= SceneManager.getInstance().norSceneView.parent.localToGlobal(point);
			
			var sceneLogic:ScenePlayerLogic	=	SceneManager.getInstance().norSceneView.scenePlayerLogic;
			var snow:BasePlayerModel	=	sceneLogic.getScenePlayerByID(arr[1]);
			var boss:NpcModel	=	sceneLogic.getScenePlayerByID(arr[2]) as NpcModel;
			if(snow && boss)
			{
				var birthPoint:Point	=  boss.effectSpace.globalToLocal( changePos(snow.playerMovie).add(globalPoint) ).add(new Point(0,-150));
				
				var deathPoint:Point	=	(birthPoint.x<=0)?new Point(10,-160):new Point(50,-110);
				var target:DisplayObject = boss.effectSpace;
			
				var xp:int	=	boss.x;
				var yp:int	=	boss.y;
				
				if((snow as NpcModel).playerMovie.currentLabel!=PlayerActionFlag.FarAttack)
				{
					(snow as NpcModel).status=PlayerActionFlag.FarAttack;
					(snow as NpcModel).playerMovie.addFrameScript(82,bombEffect);
				}
				else
				{
					bombEffect();
				}
			}
			
			function bombEffect():void
			{
				if(snow && boss && !AttackLogic.getInstance().fighting)
				{
					var offP:Point=new Point(xp-boss.x,yp-boss.y);
					birthPoint.offset(offP.x,offP.y);
					if(!isBomb)
					{
						isBomb	=	true;
					}
					else
					{
						isBomb	=	false;
					}
					new AttackTrack(boss.effectSpace,ObjectPool.GetPool(SnowBall).borrowObject() as SnowBall, birthPoint,deathPoint,target,1000,0.15,false,isBomb,null,function onHit():void
					{
						var hitPoint:Point	=	new Point(0,-150);
						if(arr[3] > 0)
						{
							var currentTime		:int		=	getTimer();
							
							if(currentTime - lastTime>gapNumTime)
							{
								ShowDamageNum.ShowDamageEffect( arr[3] , hitPoint ,boss.effectSpace ,0,100);
							}
							
							if(currentTime - lastTime>gapTime)
							{
								boss.killMove();
								boss.status	=	PlayerActionFlag.Bruise;
							}
							lastTime	=	currentTime;
						}
					});
					isBomb	=	true;
				}
			}
			
			function changePos(dis:DisplayObject):Point
			{
				if(dis)
				{
					return dis.parent.localToGlobal(new Point(dis.x,dis.y)).subtract(globalPoint);
				}
				return null;
			}
		}
	}
}